package com.prawn.ift.ctrlequipment.service.impl;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.prawn.ift.ctrlequipment.dao.CtrlequipmentDao;
import com.prawn.ift.ctrlequipment.dao.CtrlequipmentTypeDao;
import com.prawn.ift.ctrlequipment.entity.CtrlEquipmentVo;
import com.prawn.ift.ctrlequipment.pojo.Ctrlequipment;
import com.prawn.ift.ctrlequipment.pojo.CtrlequipmentType;
import com.prawn.ift.ctrlequipment.pojo.Scene;
import com.prawn.ift.ctrlequipment.service.CtrlEquipmentService;
import com.prawn.ift.ctrlequipment.service.client.CollequipmentClient;
import entity.PageResult;
import entity.Result;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.NullAttributeUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/21
 * @DESC： 控制设备服务实现
 */
@Service
public class CtrlEquipmentServiceImpl implements CtrlEquipmentService {

    @Autowired
    private CtrlequipmentDao ctrlEquipmentDao;
    @Autowired
    private CtrlequipmentTypeDao ctrlEquipmentTypeDao;
    @Autowired
    private CollequipmentClient collequipmentClient;

    /**
     * 添加控制设备信息
     *
     * @param ctrlEquipment
     * @return Result
     */
    @Override
    public Result addCtrlEquipment(Ctrlequipment ctrlEquipment) {
        if (ctrlEquipmentDao.save(ctrlEquipment) != null) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    /**
     * 删除控制设备信息
     *
     * @param id
     * @return Result
     */
    @Override
    public Result deleteCtrlEquipment(String id) {
        ctrlEquipmentDao.deleteById(id);
        return Result.success();

    }

    /**
     * 修改控制设备信息
     *
     * @param ctrlEquipment
     * @return Result
     */
    @Override
    public Result editCtrlEquipment(Ctrlequipment ctrlEquipment) {

        Ctrlequipment oldCtrlEquipment = ctrlEquipmentDao.findById(ctrlEquipment.getId()).get();
        BeanUtils.copyProperties(ctrlEquipment, oldCtrlEquipment, NullAttributeUtils.getNullPropertyNames(ctrlEquipment));
        ctrlEquipmentDao.save(oldCtrlEquipment);
        return Result.success();
    }

    //数据处理
    private  Ctrlequipment addPropertyValue(Ctrlequipment ctrlEquipment){
        if (ctrlEquipment == null)
            return null;
        if (ctrlEquipment.getTypeId() != null) {
            CtrlequipmentType ctrlEquipmentType = ctrlEquipmentTypeDao.findById(ctrlEquipment.getTypeId()).get();
            ctrlEquipment.setTypeName(ctrlEquipmentType.getName());
        }
        if (ctrlEquipment.getSceneId() != null) {
//            Result result = collequipmentClient.getEquimentById(ctrlEquipment.getSceneId());
//            ObjectMapper mapper = new ObjectMapper();
//            try {
//                String json = mapper.writeValueAsString(result.getData());
//                //json字符串转成对象
//                Scene scene = mapper.readValue(json, Scene.class);
//                ctrlEquipment.setSceneryName(scene.getName());
//
//            } catch (java.io.IOException e) {
//                e.printStackTrace();
//            }
            Map scene = (Map<Object,Object>)collequipmentClient.getEquimentById(ctrlEquipment.getSceneId()).getData();

            ctrlEquipment.setSceneryName(scene.get("name")+"");

        }
        return ctrlEquipment;
    }



    /**
     * 查询控制设备信息
     *
     * @param id
     * @return Ctrlequipment
     */
    @Override
    public Ctrlequipment getCtrlEquipmentById(String id) {
        Ctrlequipment ctrlEquipment = ctrlEquipmentDao.findById(id).get();

        ctrlEquipment=addPropertyValue(ctrlEquipment);

        return ctrlEquipment;
    }



    /**
     * 查询控制设备信息列表
     *
     * @return Result
     */
    @Override
    public Result getCtrlEquipmentList() {
        List<Ctrlequipment> ctrlEquipments = ctrlEquipmentDao.findAll();
        if (ctrlEquipments == null)
            return null;
        for (Ctrlequipment ctrlEquipment : ctrlEquipments) {
            if (ctrlEquipment.getTypeId() != null) {
                ctrlEquipment= addPropertyValue(ctrlEquipment);

            }
        }

        return Result.success().add(ctrlEquipments);
    }

    /**
     * 多条件查询控制设备信息列表
     *
     * @return Result
     */
    @Override
    public Result getCtrlEquipmentListByVo(CtrlEquipmentVo ctrlEquipmentVo) {
        List<Ctrlequipment> ctrlEquipmentList = ctrlEquipmentDao.findAll(
                createSpecificationOfCtrlEquipmentVo(ctrlEquipmentVo)
        );
        if (ctrlEquipmentList == null)
            return null;
        for (Ctrlequipment ctrlEquipment : ctrlEquipmentList) {
            if (ctrlEquipment.getTypeId() != null) {
                ctrlEquipment= addPropertyValue(ctrlEquipment);
            }
        }


        return Result.success().add(ctrlEquipmentList);
    }

    /**
     * 分页查询控制设备信息列表
     *
     * @param page
     * @param rows
     * @return Result
     */
    @Override
    public Result getCtrlEquipmentPage(int page, int rows) {
        PageRequest pageRequest = PageRequest.of(page - 1, rows);

        Page<Ctrlequipment> pageList = ctrlEquipmentDao.findAll(pageRequest);

        if (pageList == null)
            return null;
        for (Ctrlequipment ctrlEquipment : pageList.getContent()) {
            if (ctrlEquipment.getTypeId() != null) {
                ctrlEquipment= addPropertyValue(ctrlEquipment);
            }

        }


        //查询控制设备对应的按钮列表
        PageResult pageResult = new PageResult(pageList.getTotalElements(), pageList.getContent());
        return Result.success().add(pageResult);
    }


    private Specification<Ctrlequipment> createSpecificationOfCtrlEquipmentVo(CtrlEquipmentVo ctrlEquipmentVo) {

        return new Specification<Ctrlequipment>() {

            @Override
            public Predicate toPredicate(Root<Ctrlequipment> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();

                if (ctrlEquipmentVo.getName() != null && !"".equals(ctrlEquipmentVo.getName())) {
                    predicateList.add(cb.like(root.get("name").as(String.class), ctrlEquipmentVo.getName()));
                }
                if (ctrlEquipmentVo.getNumber() != null && !"".equals(ctrlEquipmentVo.getNumber())) {
                    predicateList.add(cb.equal(root.get("number").as(String.class), ctrlEquipmentVo.getNumber()));
                }
                if (ctrlEquipmentVo.getCtrlChannelId() != null && !"".equals(ctrlEquipmentVo.getCtrlChannelId())) {
                    predicateList.add(cb.equal(root.get("ctrlChannelId").as(String.class), ctrlEquipmentVo.getCtrlChannelId()));
                }
                if (ctrlEquipmentVo.getCtrlChannelNo() != null && !"".equals(ctrlEquipmentVo.getCtrlChannelNo())) {
                    predicateList.add(cb.equal(root.get("ctrlChannelNo").as(String.class), ctrlEquipmentVo.getCtrlChannelNo()));
                }
                if (ctrlEquipmentVo.getCtrlChannelName() != null && !"".equals(ctrlEquipmentVo.getCtrlChannelName())) {
                    predicateList.add(cb.like(root.get("ctrlChannelName").as(String.class), ctrlEquipmentVo.getCtrlChannelName()));
                }
                if (ctrlEquipmentVo.getSerial() != null && !"".equals(ctrlEquipmentVo.getSerial())) {
                    predicateList.add(cb.like(root.get("serial").as(String.class), ctrlEquipmentVo.getSerial()));
                }
                if (ctrlEquipmentVo.getSceneId() != null && !"".equals(ctrlEquipmentVo.getSceneId())) {
                    predicateList.add(cb.equal(root.get("sceneId").as(String.class), ctrlEquipmentVo.getSceneId()));
                }
                if (ctrlEquipmentVo.getTypeId() != null && !"".equals(ctrlEquipmentVo.getTypeId())) {
                    predicateList.add(cb.equal(root.get("typeId").as(String.class), ctrlEquipmentVo.getTypeId()));
                }
                if (ctrlEquipmentVo.getEquipmentId() != null && !"".equals(ctrlEquipmentVo.getEquipmentId())) {
                    predicateList.add(cb.equal(root.get("equipmentId").as(String.class), ctrlEquipmentVo.getEquipmentId()));
                }
                if (ctrlEquipmentVo.getEquipmentName() != null && !"".equals(ctrlEquipmentVo.getEquipmentName())) {
                    predicateList.add(cb.like(root.get("equipmentName").as(String.class), ctrlEquipmentVo.getEquipmentName()));
                }
                if (ctrlEquipmentVo.getModel() != null && !"".equals(ctrlEquipmentVo.getModel())) {
                    predicateList.add(cb.equal(root.get("model").as(String.class), ctrlEquipmentVo.getModel()));
                }
                if (ctrlEquipmentVo.getCreateDate() != null && !"".equals(ctrlEquipmentVo.getCreateDate())) {
                    predicateList.add(cb.equal(root.get("createDate").as(String.class), ctrlEquipmentVo.getCreateDate()));
                }
                if (ctrlEquipmentVo.getDelFlag() != null && !"".equals(ctrlEquipmentVo.getDelFlag())) {
                    predicateList.add(cb.equal(root.get("delFlag").as(String.class), ctrlEquipmentVo.getDelFlag()));
                }
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }


}
